package com.example.Arithmetic.Lettcode;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * 日期：2023/12/25
 * 时间：20:53
 * 描述：力扣49题
 * 找出相同重排序的字符串，并分组
 * 解题：先给字符串排序，从map中获取，获取不到新增一个分组
 */
public class E01Lettcode49_2 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String key = new String(chars);
            List<String> list = map.computeIfAbsent(key, k -> new ArrayList());
            list.add(s);
        }
        return new ArrayList<>(map.values());
    }

    @Test
    public void tets() {
        String[] s = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> lists = groupAnagrams(s);
        System.out.println(lists);
    }
}

